<template>
  <t-modal
    :title="filterTitleName"
    width="950px"
    :visible="visible"
    @cancel="closeModal()"
    @ok="handleOk()"
    wrapClassName="addTreeModal-container"
    v-bind="footer"
    forceRender
  >
    <div class="curve-modal-body">
      <a-form-model ref="ruleForm" :model="form" :rules="rules">
        <a-form-model-item label="节点名称" prop="nodeName">
          <a-input v-model="form.nodeName"></a-input>
        </a-form-model-item>
      </a-form-model>
    </div>
  </t-modal>
</template>

<script>
import { addTreeMaterial, updateTreeMaterial } from "@/api/knowledgeBase";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    footer: {
      type: Object,
      default() {
        return {};
      },
    },
    operateType: {
      type: [Number, String],
      default: 1,
    },
    parameter: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        nodeName: "",
      },
      rules: {
        nodeName: [
          {
            required: true,
            message: "请输入节点名称",
          },
        ],
      },
    };
  },
  computed: {
    filterTitleName() {
      let { operateType } = this;
      switch (Number(operateType)) {
        case 1:
          return "新增";

        case 2:
          return "编辑";

        default:
          return "新增";
      }
    },
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          let { uuid, nodeName } = this.parameter;
          if (uuid) {
            this.form.nodeName = nodeName;
          }
        }
      },
      immediate: true,
    },
  },
  methods: {
    addTreeMaterial,
    updateTreeMaterial,
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { pid, tenantCode, uuid } = this.parameter;
          let { nodeName } = this.form;
          let formJson = {
            pid,
            tenantCode,
            nodeName,
          };
          let pathUrl = "";
          //编辑
          if (uuid) {
            formJson.uuid = uuid;
            pathUrl = "updateTreeMaterial";
          }
          //新增
          else {
            pathUrl = "addTreeMaterial";
          }
          this[pathUrl](formJson)
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                this.$message.success("操作成功", 1.5);
                this.$parent.getTreeData();
                this.closeModal();
              } else {
                this.$message.error(res?.message ?? "操作失败");
              }
            })
            .catch();
        }
      });
    },
    //重置表单
    resetForm() {
      this.form = {
        nodeName: "",
      };
      this.$refs.ruleForm.resetFields();
    },
    //关闭
    closeModal() {
      this.resetForm();
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.addTreeModal-container {
  .curve-modal-body {
  }
  .ant-form-item {
    display: flex;
    .ant-form-item-label {
      flex-shrink: 0;
      width: 100px;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
    }
  }
}
</style>
